{% extends "base.html" %}

{% block js %}

/**
 * Display Map when load event
 */
var map = null;
function dispMap() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("mainMap"));
        map.setCenter(new GLatLng(35.507456, 139.617585), 13);
        map.addControl(new GMapTypeControl());
        map.addControl(new GOverviewMapControl());
        map.addControl(new GLargeMapControl());

        var latlng = null;
        var body = null;
        // published
        {% for result in published_results %}
            latlng = new GLatLng({{ result.lon }}, {{ result.lat }});
            body = "<div class=\"mapWindow\">" +
                "<a href=\"key/{{ result.key }}\">" +
                "<h1>{{ result.title }}</h1></a>"   +
                "{{ result.memo|linebreaks }}" +
                "<div><a href=\"/edit/{{ result.key }}\" onclick=\"map.closeInfoWindow();return GB_showCenter('{{ result.title }} の編集', this.href, 640, 600)\">" +
                "<img src=\"/img/edit.png\" class=\"mapIcon\" />編集</a>" +
                "<a href=\"javascript:checkDel('{{ result.title }}', '{{ result.key }}');\">" +
                "<img src=\"/img/trash.png\" class=\"mapIcon\"/>削除</a></div>" +
                "<div>登録日: {{ result.created|window_date }}</div>" +
                "</div>";
            map.addOverlay(addMarker(latlng, body));
        {% endfor %}
        
        // not published
        {% for result in private_results %}
            latlng = new GLatLng({{ result.lon }}, {{ result.lat }});
            body = "<div class=\"mapWindow\">" +
                "<a href=\"key/{{ result.key }}\">" +
                "<h1>*{{ result.title }}</h1></a>"   +
                "{{ result.memo|linebreaks }}" +
                "<div><a href=\"/edit/{{ result.key }}\" onclick=\"return GB_showCenter('{{ result.title }} の編集', this.href, 640, 600)\">" +
                "<img src=\"/img/edit.png\" class=\"mapIcon\" />編集</a>" +
                "<a href=\"javascript:checkDel('{{ result.title }}', '{{ result.key }}');\">" +
                "<img src=\"/img/trash.png\" class=\"mapIcon\"/>削除</a></div>" +
                "<div>登録日: {{ result.created|window_date }}</div>" +
                "</div>";
            map.addOverlay(addMarker(latlng, body));
        {% endfor %}
        
    }
}

/**
 * Regist Marker
 */
function addMarker(point, body) {
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(body);
    });
    return marker;
}

function checkDel(title, key) {
    var message = '「' + title + '」を削除しますか？';
    if(confirm(message)) {
        $.ajax({
            url : '/del/' + key,
            type : 'get',
            complete : function(){
                parent.parent.location.reload();
            }
        });
    }
}

window.addEventListener('load', dispMap, false);

function reloadParentOnClose(){
    window.top.location.reload();
} 
{% endblock %}

{% block body %}

<div id="content">
    
    <p>「登録」ボタンから地図情報を登録する事ができます。</p>
    
    <div id="regist">
        <a href="/add" title="地図の登録" rel="gb_page_center[600,640]">登録</a>
    </div>
    
    <div id="mainMap">&nbsp;</div>
    
</div>

{% endblock %}
